var MIN_LENGTH = 5;
var LOGIN_FIELD_ID = 'loginFieldId';
var OLD_PASSWORD_FIELD_ID = 'oldPasswordFieldId';
var PASSWORD_FIELD_ID1 = 'passwordFieldId1';
var PASSWORD_FIELD_ID2 = 'passwordFieldId2';
var FIRST_NAME_FIELD_ID = 'firstNameFieldId';
var LAST_NAME_FIELD_ID = 'lastNameFieldId';
var EMAIL_FIELD_ID = 'emailFieldId';
var SAVE_PROFILE_BUTTON_ID = 'saveProfileButtonId';
var ERRORS_LIST_ID = 'errorsListId';

$(document).ready(function(){
	$('.form-button-logout').css('display', 'block');
	$('.form-field').attr('maxlength', 15);
	$('#'+EMAIL_FIELD_ID).attr('maxlength', 25);
	$('#'+LOGIN_FIELD_ID).attr('readOnly', true);
	
	$('.form-field').bind('keyup blur',function(event){validate(this);});
	$('.registration-form-field-label').each(function(i, f){
		fieldLabelsMap[$('#'+f.id).attr('for')]=$('#'+f.id).text();
	});
	
	$('#'+SAVE_PROFILE_BUTTON_ID).click(function(event){
		validateBeforeSubmit();				
	});
	
	$('#clearButtonId').click(function(event){
		clearFields();				
	});
});

//clear alll fields and bring screen to default state
function clearFields(){
	fieldChangesMap = new Array();
	validateFields();
	$('.form-field').removeClass('form-field-valid');
	$('.form-field').removeClass('form-field-invalid');
	$('.form-field').val('');
}

function validate(field){
	console.log('validate '+field.id);
	if(fieldChangesMap[field.id] == null){
		fieldChangesMap[field.id] = true;
	}
	validateFields();
};

//validates all fields before submitting form to server
function validateBeforeSubmit(){
	$('.form-field').each(function(i, f){
		if(fieldChangesMap[f.id] == null){
			fieldChangesMap[f.id] = true;
		}
	});
	if(validateFields()){
		$('#userProfileFormId').submit();
	}
}

function validateFields(){
	var validTotal = true;
	var validField = null;
	var errorCount = 0;
	var passwordChange = true;
	var passwordNotEqual = false;
	$('#'+ERRORS_LIST_ID).empty();
	$('.form-field').each(function(i, f){
		if(fieldChangesMap[f.id] == true){
			validField = true;
			
			if($.trim($('#'+PASSWORD_FIELD_ID1).val()).length == 0 
				&& $.trim($('#'+PASSWORD_FIELD_ID2).val()).length == 0){
				passwordChange = false;
			}
			
			//check for blank
			if(f.value.length == 0){
				if(passwordChange == false && (f.id == PASSWORD_FIELD_ID1 || f.id == PASSWORD_FIELD_ID2)){
					//TODO nothing
				}else{
					validField = false;
					errorCount++;
					$('#'+ERRORS_LIST_ID).append('<li>'+messages['message.error.field']+' <strong>'+fieldLabelsMap[f.id]+'</strong> '+messages['message.error.empty']+'</li>');
				}
			}
			
			//check for min length
			if(validField){
				if(passwordChange == false && (f.id == PASSWORD_FIELD_ID1 || f.id == PASSWORD_FIELD_ID2)){
					//TODO nothing
				}else{
					if(f.value.length <= MIN_LENGTH && 
						(f.id == LOGIN_FIELD_ID || f.id == PASSWORD_FIELD_ID1 || f.id == PASSWORD_FIELD_ID2)){
						validField = false;
						errorCount++;
						$('#'+ERRORS_LIST_ID).append('<li>'+messages['message.error.field.length']+' <strong>'+fieldLabelsMap[f.id]+'</strong> '+messages['message.error.length.greater']+' '+MIN_LENGTH+'</li>');
					}
				}
			}
			
			//regexp and password equal test
			if(validField){
				if(f.id == EMAIL_FIELD_ID){
					if(!regexpEmail.test($.trim(f.value))){
						validField = false;
						errorCount++;
						$('#'+ERRORS_LIST_ID).append('<li>'+messages['message.error.field.email.incorrect']+'</li>');
					}
				}else if(f.id == PASSWORD_FIELD_ID1 || f.id == PASSWORD_FIELD_ID2){
					if($.trim($('#'+PASSWORD_FIELD_ID1).val()).length > 0 && 
					   $.trim($('#'+PASSWORD_FIELD_ID2).val()).length > 0 && 
					   $.trim($('#'+PASSWORD_FIELD_ID1).val()) !== $.trim($('#'+PASSWORD_FIELD_ID2).val()) &&
					   passwordNotEqual == false){
							validField = false;
							passwordNotEqual = true;
							errorCount++;
							$('#'+ERRORS_LIST_ID).append('<li>'+messages['message.error.passwords.not.equal']+'</li>');
					}
				}else{
					if(!isValidText($.trim(f.value))){
						validField = false;
						errorCount++;
						$('#'+ERRORS_LIST_ID).append('<li>'+messages['message.error.field']+' <strong>'+fieldLabelsMap[f.id]+'</strong> '+messages['message.error.field.contain.incorrect.symbols']+'</li>');
					}	
				}
			}
			
			if(!validField){
				validTotal=false;
				$('#'+f.id).removeClass('form-field-valid');
				$('#'+f.id).addClass('form-field-invalid');
			}else{
				$('#'+f.id).removeClass('form-field-invalid');
				$('#'+f.id).addClass('form-field-valid');
			}
		}
	});
	if(!validTotal){
		var h = 100+20*errorCount;
		$('.registration-form-error-wrapper').css('height', h+'px');
		$('.registration-form-error-wrapper').css('display', 'inline-block');
		disableButton(SAVE_PROFILE_BUTTON_ID);
		return false;
	}else{
		$('.registration-form-error-wrapper').css('display', 'none');
		enableButton(SAVE_PROFILE_BUTTON_ID);
		return true;
	}
};